package com.guchenbo.example.thread;

import com.guchenbo.example.lock.WaitClientCloseServer;

import java.util.concurrent.CountDownLatch;

public class ThreadOrderDemo {
    public static void main(String[] args) {
        ThreadOrderDemo demo = new ThreadOrderDemo();
//        demo.test1();
//        demo.test2();


        CountDownLatch latch = new CountDownLatch(1);
        latch.countDown();
        try {
            latch.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("end");
    }

    private void test2() {
        CountDownLatch latch = new CountDownLatch(1);
        Thread t1 = GcbThreadFactory.create("t1", () -> {
            System.out.println(Thread.currentThread().getName() + " 执行开始 ");
            new WaitClientCloseServer().start(10000);
            System.out.println(Thread.currentThread().getName() + " 执行结束 ");
            latch.countDown();

        });
        Thread t2 = GcbThreadFactory.create("t2", () -> {
            try {
                System.out.println(Thread.currentThread().getName() + " t1.join ");
                latch.await();
                System.out.println(Thread.currentThread().getName() + " 继续执行 ");
                //                new WaitClientCloseServer().start(10001);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        t1.start();
        t2.start();
    }

    private void test1() {
        Thread t1 = GcbThreadFactory.create("t1", () -> {
            System.out.println(Thread.currentThread().getName() + " 执行开始 ");
            new WaitClientCloseServer().start(10000);
            System.out.println(Thread.currentThread().getName() + " 执行结束 ");

        });
        Thread t2 = GcbThreadFactory.create("t2", () -> {
            try {
                System.out.println(Thread.currentThread().getName() + " t1.join ");
                t1.join();
                System.out.println(Thread.currentThread().getName() + " 继续执行 ");
//                new WaitClientCloseServer().start(10001);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });


        t1.start();
        // t1 join之前需要先启动
        t2.start();

    }
}
